package mcia_administrative;

import java.io.*;
import java.util.*;
import javax.swing.DefaultListModel;
import javax.swing.UIManager;
import java.io.File;

/**
 * ScenesForCL.java - frame that allows user to add local scenes into CL
 *
 * @author MCIA
 */
public class ScenesForCL extends javax.swing.JFrame {

    static final String strDirectory = "temp";
    static File scenes = new File(strDirectory);
    private String currentCL = DesktopFloorDesign.getCurrentCL();               //name of CL for which LS are being edited
    private int currentCLnum = DesktopFloorDesign.getCLnum();                   //number of CL for which LS are being edited
    private int currentFloorNum = DesktopFloorDesign.getFloorNum();             //getting current floor number
    public String currentUser = "admin";                                        //name of current user (get from other window or set still)
    private String[][] LSdata;                                                  //keeps all scenes data while program running
    private String[][] rFile;                                                   //keeps data from profile.txt file
    DefaultListModel listModel = new DefaultListModel();
    int elements = 0, lines = 0;

    /** Creates new form ScenesForCL */
    public ScenesForCL() {
        initComponents();

        if (listModel.isEmpty()) {
            addLightButton.setEnabled(false);
            deletelightButton.setEnabled(false);
            SavePredef.setEnabled(false);
            saveCurrentSettingsButton.setEnabled(false);
            renameScene.setEnabled(false);
            removeScene.setEnabled(false);
        }
        SceneLabel.setText("Scenes for " + currentCL);
        //setting text to status label
        StatusLabel.setText("User: " + currentUser + " |  " + currentCL);
        //setting value for scrollbar's description
        brightnessScrollBar.setValue(Integer.parseInt(brightnessTextField.getText()));
        for (int n = 0; n < Main.allLights.size(); n++) {
            if (Main.allLights.get(n)[0].equals(currentCL)) {
                LightList.addItem(Main.allLights.get(n)[1]);
            }
        }
        //creating scenes directory
        try {
            boolean success = (scenes).mkdir();
            if (success) {
                System.out.println("Directory " + strDirectory + " created:" + scenes.getAbsolutePath());
            } else {
                System.out.println("Directory: " + strDirectory + " not created, probably it exists.");
            }
        } catch (Exception e) { //catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        jSpinner1.setEnabled(false);
        brightnessScrollBar.setEnabled(false);
        brightnessTextField.setEnabled(false);
        scenesList.setModel(listModel);
        LSdata = new String[100][100];

        //reads previously defined settings if they exist
        readSavedDataFile();
        //reading scenes/profile.txt for loading predefined scenes
        readConfigFile();

        if (predefinedList.getItemCount() == 0) {
            addSceneButton.setEnabled(false);
            predefinedList.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ClosingConfirmation = new javax.swing.JFrame();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        OK = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        SceneLabel = new javax.swing.JLabel();
        DescriptionLabel = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        StatusLabel = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        scenesList = new javax.swing.JList();
        renameSceneTextField = new javax.swing.JTextField();
        renameScene = new javax.swing.JButton();
        removeScene = new javax.swing.JButton();
        newSceneLabel = new javax.swing.JLabel();
        newSceneField = new javax.swing.JTextField();
        createSceneButton = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lightsList = new javax.swing.JTable();
        LightList = new javax.swing.JComboBox();
        addLightButton = new javax.swing.JButton();
        deletelightButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        brightnessTextField = new javax.swing.JFormattedTextField();
        brightnessScrollBar = new javax.swing.JScrollBar();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        saveCurrentSettingsButton = new javax.swing.JButton();
        saveAndExitButton = new javax.swing.JButton();
        backButton = new javax.swing.JButton();
        predefinedList = new javax.swing.JComboBox();
        addSceneButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        SavePredef = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();

        ClosingConfirmation.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        ClosingConfirmation.setTitle("Test");
        ClosingConfirmation.setAlwaysOnTop(true);
        ClosingConfirmation.setBounds(new java.awt.Rectangle(500, 350, 0, 0));
        ClosingConfirmation.setMinimumSize(new java.awt.Dimension(318, 153));
        ClosingConfirmation.setName("closeConfirmation"); // NOI18N

        jPanel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setText("Do you really want to quit?");

        OK.setText("OK");
        OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKActionPerformed(evt);
            }
        });

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(Cancel))
                    .addComponent(jLabel2))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel2)
                .addGap(36, 36, 36)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Cancel)
                    .addComponent(OK))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ClosingConfirmationLayout = new javax.swing.GroupLayout(ClosingConfirmation.getContentPane());
        ClosingConfirmation.getContentPane().setLayout(ClosingConfirmationLayout);
        ClosingConfirmationLayout.setHorizontalGroup(
            ClosingConfirmationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        ClosingConfirmationLayout.setVerticalGroup(
            ClosingConfirmationLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setBounds(new java.awt.Rectangle(200, 20, 800, 600));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        SceneLabel.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        SceneLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        SceneLabel.setText("Local Scene for CL");

        DescriptionLabel.setForeground(new java.awt.Color(51, 102, 255));
        DescriptionLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        DescriptionLabel.setText("Description Label");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(DescriptionLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE)
                    .addComponent(SceneLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 784, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(SceneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(DescriptionLabel)
                .addContainerGap())
        );

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setMaximumSize(new java.awt.Dimension(800, 32767));
        jPanel1.setPreferredSize(new java.awt.Dimension(700, 30));

        StatusLabel.setText("Edit local scenes");
        StatusLabel.setOpaque(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(StatusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(StatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("List of scenes added to CL:");
        jLabel1.setOpaque(true);

        scenesList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        scenesList.setPreferredSize(new java.awt.Dimension(30, 30));
        scenesList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                scenesListMousePressed(evt);
            }
        });
        scenesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                scenesListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(scenesList);

        renameSceneTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameSceneTextFieldActionPerformed(evt);
            }
        });

        renameScene.setText("Rename");
        renameScene.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                renameSceneActionPerformed(evt);
            }
        });

        removeScene.setText("Remove Scene");
        removeScene.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeSceneActionPerformed(evt);
            }
        });

        newSceneLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        newSceneLabel.setText("New Scene Name:");
        newSceneLabel.setOpaque(true);

        newSceneField.setText("New Scene");

        createSceneButton.setText("Create New Scene");
        createSceneButton.setPreferredSize(new java.awt.Dimension(50, 30));
        createSceneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createSceneButtonActionPerformed(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel11.setText("Lights edit:");
        jLabel11.setOpaque(true);

        lightsList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Light number", "State", "Timer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        lightsList.setPreferredSize(new java.awt.Dimension(300, 500));
        lightsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lightsList.getTableHeader().setReorderingAllowed(false);
        lightsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lightsListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lightsList);

        LightList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LightListActionPerformed(evt);
            }
        });

        addLightButton.setText("Add Light");
        addLightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLightButtonActionPerformed(evt);
            }
        });

        deletelightButton.setText("Delete Light");
        deletelightButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deletelightButtonActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 13));
        jLabel5.setForeground(new java.awt.Color(51, 204, 0));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Light number 0 is edited");
        jLabel5.setOpaque(true);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("Light Brightness [%] :");
        jLabel6.setOpaque(true);

        brightnessTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        brightnessTextField.setText("100");
        brightnessTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                brightnessTextFieldActionPerformed(evt);
            }
        });

        brightnessScrollBar.setOrientation(javax.swing.JScrollBar.HORIZONTAL);
        brightnessScrollBar.setVisibleAmount(0);
        brightnessScrollBar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        brightnessScrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
                brightnessScrollBarAdjustmentValueChanged(evt);
            }
        });

        jSpinner1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jSpinner1.setModel(new javax.swing.SpinnerNumberModel(0, 0, 3600, 1));
        jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSpinner1StateChanged(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel7.setText("Timer:");
        jLabel7.setOpaque(true);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel8.setText("sec.");
        jLabel8.setOpaque(true);

        saveCurrentSettingsButton.setText("Save Current Settings");
        saveCurrentSettingsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCurrentSettingsButtonActionPerformed(evt);
            }
        });

        saveAndExitButton.setText("Save & Exit");
        saveAndExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAndExitButtonActionPerformed(evt);
            }
        });

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        predefinedList.setOpaque(false);
        predefinedList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                predefinedListActionPerformed(evt);
            }
        });

        addSceneButton.setText("Add Scene From The List");
        addSceneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addSceneButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Adding Predefined Scenes:");

        SavePredef.setText("Save Predefined Schema");
        SavePredef.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SavePredefActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(saveCurrentSettingsButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(brightnessScrollBar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(brightnessTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                    .addComponent(jLabel7)
                                    .addGap(18, 18, 18)
                                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(29, 29, 29)
                                    .addComponent(jLabel8)
                                    .addGap(28, 28, 28)))))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(saveAndExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(12, Short.MAX_VALUE))
            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(27, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(SavePredef, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addSceneButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(predefinedList, javax.swing.GroupLayout.Alignment.LEADING, 0, 169, Short.MAX_VALUE))
                .addGap(22, 22, 22))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(brightnessTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(brightnessScrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveCurrentSettingsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(predefinedList, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addSceneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SavePredef, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(saveAndExitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(createSceneButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(removeScene, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(renameScene, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(renameSceneTextField)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                    .addComponent(newSceneLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(newSceneField))
                .addGap(57, 57, 57)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(deletelightButton, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addComponent(LightList, 0, 248, Short.MAX_VALUE)
                    .addComponent(jLabel11)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                    .addComponent(addLightButton, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE))
                .addGap(37, 37, 37)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(renameSceneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(renameScene, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(removeScene, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(newSceneLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(newSceneField, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createSceneButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(15, 15, 15))))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LightList, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(addLightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deletelightButton, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void readConfigFile() {
        int maxelements = 0;
        File sourcefile = new File("scenes", "profile.txt");
        try {

            FileReader fr = new FileReader(sourcefile);
            BufferedReader br = new BufferedReader(fr);
            System.out.println("Sucessfully accessed txt file: " + sourcefile);
            String dbRecord1;

            while ((dbRecord1 = br.readLine()) != null) {
                lines++;
                StringTokenizer st = new StringTokenizer(dbRecord1, ";");
                maxelements = st.countTokens();
                if (maxelements > elements) {
                    elements = maxelements;
                }
            }
        } catch (Exception ex) {
            System.out.println("Error with file: " + ex + " Filename: ");
        }
        File sourcefile1 = new File("scenes", "profile.txt");

        rFile = new String[100][100];
        try {
            FileReader fr1 = new FileReader(sourcefile1);
            BufferedReader br = new BufferedReader(fr1);
            String dbRecord;
            //reading txt config file
            boolean existAlready = false;
            int line = 0;
            while ((dbRecord = br.readLine()) != null) {
                int token = 0;
                StringTokenizer st = new StringTokenizer(dbRecord, ";");
                while (st.hasMoreElements()) {
                    rFile[line][token] = st.nextToken();
                    token++;
                }
                for (int i = 0; i < predefinedList.getItemCount(); i++) {
                    predefinedList.setSelectedIndex(i);
                    if (predefinedList.getSelectedItem().toString().equals(rFile[line][0])) {
                        existAlready = true;
                    }
                }
                for (int i = 0; i < scenesList.getModel().getSize(); i++) {
                    scenesList.setSelectedIndex(i);
                    if (scenesList.getSelectedValue().toString().equals(rFile[line][0])) {
                        existAlready = true;
                    }
                }
                if (!existAlready) {
                    predefinedList.addItem(rFile[line][0]);
                }
                existAlready = false;
                line++;
            }

        } catch (Exception ex) {
            System.out.println("Error with file: " + ex + " Filename: ");
        }

        if (predefinedList.getItemCount() != 0) {
            addSceneButton.setEnabled(true);
            predefinedList.setEnabled(true);
        }
    }

    private void readSavedDataFile() {
        int line = 0;

        File sourcef = new File(strDirectory, "LS" + currentFloorNum + "." + currentCLnum + ".txt");
        try { //what to do with txt file

            FileReader fread = new FileReader(sourcef);
            BufferedReader bread = new BufferedReader(fread);
            System.out.println("Sucessfully accessed txt file: " + sourcef);
            String dbRec;


            while ((dbRec = bread.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(dbRec, ";");
                int b = 0;
                while (st.hasMoreElements()) {
                    LSdata[line][b] = st.nextToken();
                    b++;
                }
                line++;
            }
        } catch (Exception ex) {
            System.out.println("Error with file: " + ex + "Reading file.");
        }
        int i = 0;
        while (LSdata[i][0] != null) {
            i++;
        }
        for (int w = 0; w < i; w++) {
            if (LSdata[w][0] != null);
            listModel.addElement(LSdata[w][0]);
        }
    }

    private int findPlace() {
        for (int r = 0; r < LSdata.length; r++) {
            if (lightsList.getValueAt(r, 0) == null) {
                return r;
            }
        }
        return -1;
    }

    private void moveData() {
        //rebuilding LSdata[][] after delete one entry
        int i = 0;
        while ((LSdata[i][0] != null && LSdata[i + 1][0] != null) || (LSdata[i][0] == null && LSdata[i + 1][0] != null) || (LSdata[i][0] != null && LSdata[i + 1][0] == null)) {
            if (LSdata[i][0] == null && LSdata[i + 1][0] != null) {
                int a = i;
                while ((LSdata[a][0] == null && LSdata[a + 1][0] != null)) {
                    int b = 0;
                    while (LSdata[a + 1][b] != null) {
                        LSdata[a][b] = LSdata[a + 1][b];
                        LSdata[a + 1][b] = null;
                        b++;
                    }
                    a++;
                }
            }
            i++;
        }
    }

    private void OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKActionPerformed
        //exit ScenesForCL
        this.dispose();
        ClosingConfirmation.dispose();
}//GEN-LAST:event_OKActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        ClosingConfirmation.setVisible(false); // hide closingConfirmation
}//GEN-LAST:event_CancelActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        ClosingConfirmation.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void removeSceneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeSceneActionPerformed
        //deleting scenes from list after selecting and pressing button
        if (scenesList.getSelectedValue() != null) {    //if list is not empty
            String word = (String) scenesList.getSelectedValue();
            listModel.removeElementAt(scenesList.getSelectedIndex());
            scenesList.setSelectedIndex(0);
            renameSceneTextField.setText("");
            int i = 0, k = 0;
            for (int v = 0; v < 6; v++) {
            }
            while (!LSdata[i][0].equals(word) || LSdata[i][0] == null) {
                i++;
            }
            while (LSdata[i][k] != null) {
                LSdata[i][k] = null;
                k++;
            }
        }

        moveData();

        //clean table light list
        if (LSdata[0][0] == null) {
            for (int j = 0; j < lightsList.getRowCount(); j++) {
                for (int x = 0; x < lightsList.getColumnCount(); ++x) {
                    lightsList.setValueAt(null, j, x);                                     //cleaning table
                }
            }
        }

        readConfigFile();

        if (scenesList.getSelectedIndex() == -1) {
            saveCurrentSettingsButton.setEnabled(false);
            addLightButton.setEnabled(false);
            deletelightButton.setEnabled(false);
            renameScene.setEnabled(false);
            removeScene.setEnabled(false);
            SavePredef.setEnabled(false);
        }
    }//GEN-LAST:event_removeSceneActionPerformed

    private void brightnessTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_brightnessTextFieldActionPerformed
        String value = brightnessTextField.getText();
        try {
            int value2 = Integer.parseInt(value);

            if (value2 < 0) {
                brightnessTextField.setText("0");
                value2 = 0;
            } else if (value2 > 100) {
                brightnessTextField.setText("100");
                value2 = 100;
            }
            brightnessScrollBar.setValue(value2);
        } catch (NumberFormatException ex) {
            System.out.println("Inserted value is not number: " + ex);
        }
    }//GEN-LAST:event_brightnessTextFieldActionPerformed

    private void brightnessScrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {//GEN-FIRST:event_brightnessScrollBarAdjustmentValueChanged
        brightnessScrollBar.setMaximum(100);
        int percent = (int) Math.round(brightnessScrollBar.getValue());
        String percent2 = Integer.toString(percent);
        brightnessTextField.setText(Integer.toString(percent));
        if ((lightsList.getSelectedRow() != -1)) {
            if (lightsList.getValueAt(lightsList.getSelectedRow(), 0) != null) {
                lightsList.setValueAt(percent2, lightsList.getSelectedRow(), 1);
            }
        }
    }//GEN-LAST:event_brightnessScrollBarAdjustmentValueChanged

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_backButtonActionPerformed

    private void scenesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_scenesListValueChanged
        lightsList.clearSelection();
        brightnessTextField.setText("0");
        brightnessScrollBar.setValue(Integer.parseInt(brightnessTextField.getText()));
        jSpinner1.setValue(0);

        if (!scenesList.isSelectionEmpty()) {
            String index = (String) listModel.get(scenesList.getSelectedIndex());
            renameSceneTextField.setText(index);
            for (int j = 0; j < lightsList.getRowCount(); j++) {
                for (int x = 0; x < lightsList.getColumnCount(); ++x) {
                    lightsList.setValueAt(null, j, x);                                     //cleaning table
                }
            }
            for (int i = 0; i < LSdata.length; i++) {
                int c = 0, d = 1;
                if (index.toString().equals(LSdata[i][0])) {
                    while (LSdata[i][d] != null) {
                        lightsList.setValueAt("" + LSdata[i][d], c, 0);                  //seting table value from LSdata[][]
                        lightsList.setValueAt("" + LSdata[i][d + 1], c, 1);
                        lightsList.setValueAt("" + LSdata[i][d + 2], c, 2);
                        c++;
                        d = d + 3;
                    }
                }
            }
        }

        if (lightsList.getSelectedRow() == -1) {
            jSpinner1.setEnabled(false);
            brightnessScrollBar.setEnabled(false);
            brightnessTextField.setEnabled(false);
        }

        if (scenesList.getSelectedIndex() != -1) {
            saveCurrentSettingsButton.setEnabled(true);
            addLightButton.setEnabled(true);
            deletelightButton.setEnabled(true);
            renameScene.setEnabled(true);
            removeScene.setEnabled(true);
            addLightButton.setEnabled(true);
            SavePredef.setEnabled(true);
        }

    }//GEN-LAST:event_scenesListValueChanged

    private void addSceneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addSceneButtonActionPerformed
        //if combobox is not empty
        if (predefinedList.getItemCount() != 0) {
            String element = (String) predefinedList.getSelectedItem();
            if (!listModel.contains(element)) {
                //add element to list and delete from combobox
                predefinedList.removeItemAt(predefinedList.getSelectedIndex());
                listModel.addElement(element);

                //copying data rFile -> LSdata
                for (int i = 0; i < rFile.length; i++) {
                    if (element.toString().equals(rFile[i][0])) {
                        int x = 0, a = 0;
                        while (LSdata[a][0] != null) {
                            a++;
                        }
                        while (rFile[i][x] != null) {
                            LSdata[a][x] = rFile[i][x];
                            x++;
                        }
                    }
                }
            } else {
                DescriptionLabel.setText("Element Already Exist");
            }
        }

        if (predefinedList.getItemCount() == 0) {
            addSceneButton.setEnabled(false);
            predefinedList.setEnabled(false);

        }
    }//GEN-LAST:event_addSceneButtonActionPerformed

    private void lightsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lightsListMouseClicked
        if (lightsList.getValueAt(lightsList.getSelectedRow(), 0) != null) {
            String name = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 0);
            String value = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 1);
            String value2 = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 2);
            brightnessScrollBar.setValue(Integer.parseInt(value));
            jSpinner1.setValue(Integer.parseInt(value2));
            jLabel5.setText("Light " + name + " is edited.");
            jSpinner1.setEnabled(true);
            brightnessScrollBar.setEnabled(true);
            brightnessTextField.setEnabled(true);
        } else {
            brightnessScrollBar.setValue(0);
            jSpinner1.setValue(0);
            jLabel5.setText("Nothing is edited.");
            jSpinner1.setEnabled(false);
            brightnessScrollBar.setEnabled(false);
            brightnessTextField.setEnabled(false);

        }
        deletelightButton.setEnabled(true);
    }//GEN-LAST:event_lightsListMouseClicked

    private void createSceneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createSceneButtonActionPerformed
        String read = newSceneField.getText();
        if (listModel.contains(read)) {
            System.out.print("Element already exist!");
            DescriptionLabel.setText("Element Already Exist");
        } else {
            listModel.addElement(read);
        }
        int a = 0;
        while (LSdata[a][0] != null) {
            a++;
        }
        LSdata[a][0] = read;

        for (int d = 0; d < 3; d++) {
            for (int g = 0; g < 5; g++) {
            }
        }

        SavePredef.setEnabled(true);

    }//GEN-LAST:event_createSceneButtonActionPerformed

    private void renameSceneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameSceneActionPerformed
        if (renameSceneTextField.getText() == null ? "" != null : !renameSceneTextField.getText().equals("")) {
            String old = (String) scenesList.getSelectedValue();
            String index = renameSceneTextField.getText();
            if (!listModel.contains(index)) {
                listModel.set(scenesList.getSelectedIndex(), index);
                int x = 0;
                while (!LSdata[x][0].equals(old)) {
                    x++;
                }
                LSdata[x][0] = index;
            } else {
                DescriptionLabel.setText("Element Already Exist");
            }
        }
    }//GEN-LAST:event_renameSceneActionPerformed

    private void LightListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LightListActionPerformed
        //
    }//GEN-LAST:event_LightListActionPerformed

    private void predefinedListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_predefinedListActionPerformed
        //
    }//GEN-LAST:event_predefinedListActionPerformed

    private void addLightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLightButtonActionPerformed
        if (LightList.getItemCount() != 0) {
            String element = (String) LightList.getSelectedItem();
            int f = findPlace();
            StringTokenizer st = new StringTokenizer(element, ".");

            st.nextToken();
            st.nextToken();
            String lightnr = st.nextToken();

            lightsList.setValueAt(lightnr, f, 0);
            lightsList.setValueAt("0", f, 1);
            lightsList.setValueAt("0", f, 2);


            //finding entry in LSdata[][] and adding:
            int a = 0, x = 0;
            while (LSdata[a][0] != scenesList.getSelectedValue() && a < LSdata.length) {
                a++;
            }
            if (LSdata[a][0] == scenesList.getSelectedValue()) {
                while (LSdata[a][x] != null) {
                    x++;
                }

                LSdata[a][x] = lightnr;
                LSdata[a][x + 1] = (String) lightsList.getValueAt(f, 1);
                LSdata[a][x + 2] = (String) lightsList.getValueAt(f, 2);
            }

        }
    }//GEN-LAST:event_addLightButtonActionPerformed

    private void deletelightButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deletelightButtonActionPerformed
        if (lightsList.getValueAt(lightsList.getSelectedRow(), lightsList.getSelectedColumn()) != null) {
            lightsList.setValueAt(null, lightsList.getSelectedRow(), 0);
            lightsList.setValueAt(null, lightsList.getSelectedRow(), 1);
            lightsList.setValueAt(null, lightsList.getSelectedRow(), 2);
            for (int b = lightsList.getSelectedRow(); b < lightsList.getRowCount() - 1; b++) {
                lightsList.setValueAt(lightsList.getValueAt(b + 1, 0), b, 0);
                lightsList.setValueAt(lightsList.getValueAt(b + 1, 1), b, 1);
                lightsList.setValueAt(lightsList.getValueAt(b + 1, 2), b, 2);
            }
        }
        if (lightsList.getValueAt(lightsList.getSelectedRow(), 0) != null) {
            String name = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 0);
            String value = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 1);
            String value2 = (String) lightsList.getValueAt(lightsList.getSelectedRow(), 2);
            brightnessScrollBar.setValue(Integer.parseInt(value));
            jSpinner1.setValue(Integer.parseInt(value2));
            jLabel5.setText(name + " is edited.");
            jSpinner1.setEnabled(true);
            brightnessScrollBar.setEnabled(true);
            brightnessTextField.setEnabled(true);
        } else {
            brightnessScrollBar.setValue(0);
            jSpinner1.setValue(0);
            jLabel5.setText("Nothing is edited.");
            jSpinner1.setEnabled(false);
            brightnessScrollBar.setEnabled(false);
            brightnessTextField.setEnabled(false);
        }
    }//GEN-LAST:event_deletelightButtonActionPerformed

    private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSpinner1StateChanged
        int value = (Integer) (jSpinner1.getValue());
        String value2 = Integer.toString(value);
        if (lightsList.getSelectedRow() != -1) {
            if (lightsList.getValueAt(lightsList.getSelectedRow(), 0) != null) {
                lightsList.setValueAt(value2, lightsList.getSelectedRow(), 2);
            }
        }
    }//GEN-LAST:event_jSpinner1StateChanged

    private void saveCurrentSettingsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCurrentSettingsButtonActionPerformed
        if (!listModel.isEmpty()) {
            String old = (String) scenesList.getSelectedValue();
            int x = 0;
            while (!old.equals(LSdata[x][0]) && x < LSdata.length - 2) {
                x++;
            }
            for (int r = 0, w = 2; r < lightsList.getRowCount(); r++, w = w + 3) {
                LSdata[x][w] = (String) lightsList.getValueAt(r, 1);//wiersz,kolumna
                LSdata[x][w + 1] = (String) lightsList.getValueAt(r, 2);
            }
    }//GEN-LAST:event_saveCurrentSettingsButtonActionPerformed
    }
    private void renameSceneTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_renameSceneTextFieldActionPerformed
        //
    }//GEN-LAST:event_renameSceneTextFieldActionPerformed

    private void SavePredefActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SavePredefActionPerformed
        File f = new File("scenes", "profile.txt");
        try {
            FileWriter fstream = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fstream);
            //for every CL
            String line;
            int g = 0;
            while (LSdata[g][0] != null) {
                g++;
            }
            for (int j = 0; j < g; j++) {
                line = "";
                int s = 0;

                while (LSdata[j][s] != null) {
                    s++;
                }
                for (int k = 0; k < s; k++) {
                    line = line + LSdata[j][k] + ";";
                }
                out.write(line);
                out.newLine();
            }
            out.close();
            System.out.println("File saved successfully: " + f);
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

    }//GEN-LAST:event_SavePredefActionPerformed

    private void scenesListMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_scenesListMousePressed
        String old = (String) scenesList.getSelectedValue();
        int x = 0;
        while (!old.equals(LSdata[x][0]) && x < LSdata.length - 2) {
            x++;
        }
        for (int r = 0, w = 2; r < lightsList.getRowCount(); r++, w = w + 3) {
            LSdata[x][w] = (String) lightsList.getValueAt(r, 1);//wiersz,kolumna
            LSdata[x][w + 1] = (String) lightsList.getValueAt(r, 2);
        }
    }//GEN-LAST:event_scenesListMousePressed

    private void saveAndExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAndExitButtonActionPerformed
        String txtName = "LS" + currentFloorNum + "." + currentCLnum + ".txt";
        File f = new File(strDirectory, txtName);
        try {
            FileWriter fstream = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fstream);
            //for every CL
            String line = "";
            for (int j = 0; j < 10; j++) {
                line = "";
                int s = 0;
                while (LSdata[j][s] != null) {
                    s++;
                }
                for (int k = 0; k < s; k++) {
                    line = line + LSdata[j][k] + ";";
                }
                System.out.println("File: " + txtName + " Saved line: " + line);
                out.write(line);
                out.newLine();
            }
            out.close();
            System.out.println("File saved successfully: " + f);
        } catch (Exception e) {//catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        this.dispose();
}//GEN-LAST:event_saveAndExitButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //setting Windows Look and Feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println("Error setting native LAF: " + ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DesktopFloorDesign thisFrame = new DesktopFloorDesign();
                String[] vars = null;
                Main.main(vars);
                Main.changeFrame(thisFrame);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancel;
    private javax.swing.JFrame ClosingConfirmation;
    private javax.swing.JLabel DescriptionLabel;
    private javax.swing.JComboBox LightList;
    private javax.swing.JButton OK;
    private javax.swing.JButton SavePredef;
    private javax.swing.JLabel SceneLabel;
    private javax.swing.JLabel StatusLabel;
    private javax.swing.JButton addLightButton;
    private javax.swing.JButton addSceneButton;
    private javax.swing.JButton backButton;
    private javax.swing.JScrollBar brightnessScrollBar;
    private javax.swing.JFormattedTextField brightnessTextField;
    private javax.swing.JButton createSceneButton;
    private javax.swing.JButton deletelightButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JTable lightsList;
    private javax.swing.JTextField newSceneField;
    private javax.swing.JLabel newSceneLabel;
    private javax.swing.JComboBox predefinedList;
    private javax.swing.JButton removeScene;
    private javax.swing.JButton renameScene;
    private javax.swing.JTextField renameSceneTextField;
    private javax.swing.JButton saveAndExitButton;
    private javax.swing.JButton saveCurrentSettingsButton;
    private javax.swing.JList scenesList;
    // End of variables declaration//GEN-END:variables
}
